package com.beamfield.controller.manage.process;

import com.alibaba.fastjson.JSON;
import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.ConcreteOpening;
import com.beamfield.modelVo.ConcreteOpeningVo;
import com.beamfield.service.manage.process.ProcessOpeningService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 开盘证
 */
@Controller
@RequestMapping(value = "/cms/process/opening")
public class ProcessOpeningController extends BaseController {

    private final ProcessOpeningService processOpeningService;

    @Autowired
    public ProcessOpeningController(ProcessOpeningService processOpeningService) {
        this.processOpeningService = processOpeningService;
    }

    @RequestMapping(value = "/opening")
    public ModelAndView opening(){
        return new ModelAndView();
    }
    /**
     * 保存开盘证
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSave",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSave(HttpServletRequest request, @RequestParam("openingJson") String openingJson){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        if(StringUtils.isEmpty(openingJson)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        int result = 0;
        try{
            ConcreteOpening concreteOpening = JSON.parseObject(openingJson, ConcreteOpening.class);
            result = processOpeningService.save(beamId, userId, concreteOpening);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"操作成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }

    /**
     * 查询开盘证
     * @param request
     * @return
     */
    @RequestMapping(value = "/readOpeningData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readOpeningData(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String noticeId = request.getParameter("notice_id");
        Long beamId = super.getBeamId(request);
        List<ConcreteOpeningVo> concreteOpeningVoList = processOpeningService.getOpening(beamId, offset, limit, noticeId);
        long total = processOpeningService.countOpening(beamId, noticeId);
        return new PageResult(total, CommonUtils.parseLong(offset, 0L), concreteOpeningVoList);
    }

    @RequestMapping(value = "/getPart", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getPart(HttpServletRequest request){
        Long beamId = super.getBeamId(request);

        return new ApiResult(processOpeningService.getPart(beamId));
    }
}
